define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');



	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-verifySweety'
			});

			// 渠道列的配置
			var exchange = pubjs.config('exchange');
			this.$metrics = [];
			util.each(exchange, function(data){
				this.$metrics.push({
					// width: 100,
					align: 'center',
					name: data.id,
					text: LANG(data.name),
					render: 'renderStatus',
					param: data.alias_id || data.id
				});
			}, this);
			this.$metrics.push({
				align: 'center',
				name: 'no_exchange',
				text: LANG('没有渠道审核结果'),
				param: 'no_exchange'
			});

			// 用户关联的资质信息
			var current = pubjs.getUserCurrent();
			var Aptitudes = current.ExchangeInfo.Aptitudes && current.ExchangeInfo.Aptitudes;
			AptitudeIds =[];
			this.$AptitudeItems = [];
			this.$def_aptitudeId = null;
			if (Aptitudes && Aptitudes.length)
			{
				util.each(Aptitudes, function(item)
				{
					if (AptitudeIds.indexOf(+item.AptitudeId) == -1)
					{
						AptitudeIds.push(+item.AptitudeId)
						this.$AptitudeItems.push({
							'id': +item.AptitudeId,
							'name': item.Aptitude
						});
						// 默认选中的资质
						this.$def_aptitudeId = this.$def_aptitudeId || +item.AptitudeId;
					}
				}, this);
			}

			this.Super('init', arguments);
		},
		onGridDataLoad: function(ev)
		{
			var data = ev.param;
			var channel_ids = data.Channel_Ids || [];
			var showIds = [];
			var hideIds = [];
			var allIds = [];
			var exchange = pubjs.config('exchange');
			// 判断是否有审核结果
			util.each(exchange, function(item)
			{
				var id = item.id;
				allIds.push(id);
				// 有审核结果的渠道，显示
				if (channel_ids.indexOf(+id) != -1)
				{
					showIds.push(id);
				}
				// 无审核结果的渠道，隐藏
				else
				{
					hideIds.push(id);
				}
			}, this);

			if (data.Channel_Ids && data.Channel_Ids.length)
			{
				// 有审核结果的渠道，显示
				this.$.grid.toggleColumn(showIds, true);
				// 无审核结果的渠道，隐藏
				this.$.grid.toggleColumn(hideIds, false);
				this.$.grid.toggleColumn('no_exchange', false);
			}
			//如果一个审核结果都没有，则显示全部渠道，避免显示问题。
			else
			{
				this.$.grid.toggleColumn(allIds, false);
				this.$.grid.toggleColumn('no_exchange', true);
			}
		},
		onDropChange: function(ev)
		{
			var data = ev.param;
			this.$.grid.setParam({'QualificationId': data.id}).reload();
		}
	});

	// 创意包审核
	var SweetyVerify= Base.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-verifySweety'
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var c = this.getConfig();

			this.append($([
				// '<div class="M-formHead">',
				// 	'<div class="M-formHeadTitle">'+LANG('创意包审核')+'</div>',
				// '</div>',
				'<div>',
					'<div class="P-verifyGridHead">',
						'<span class="" pub-mod="@base/common/drop.drop" pub-name="aptitude"></span>',
					'</div>',
					'<div pub-name="grid" pub-mod="sweety/grid/verify.sweety"/>',
				'</div>'
			].join('')));

			this.buildFromTemplate({
				'aptitude': {
					'options': this.$AptitudeItems,
					'value': this.$def_aptitudeId
				},
				'grid': {
					'hasSearch': true,
					'hasFilter': false,
					'eventDataLoad': true,
					'metrics': this.$metrics,
					'param': {
						'QualificationId': this.$def_aptitudeId
					},
					'auto_load': !!this.$def_aptitudeId
				}
			});

			// 创建侧边栏
			this.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'attr': {'data-tab': this._.name},
				'items': [

				]
			});
		},
		reload: function(){
			this.$.grid.reload();
		},
		// 操作菜单-下拉菜单界面
		onOperateMenuShow: function(ev){
			ev.returnValue = [{id: 'view', name: LANG('查看审核状态')}];
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var op = ev.param[0];
			var data = ev.param[1];

			switch(op.key){
				case 'view':
					this.showPopwin(data._id, data.Name);
				break;
			}
			return false;
		},
		// 显示弹层
		showPopwin: function(id, title){
			var aptitude = this.$.aptitude.getValue();
			var QualificationId = aptitude && aptitude.id;
			if (!QualificationId)
			{
				return false;
			}

			if(this.$.popwin){
				this.$.popwin.show().setParam(id, title, QualificationId).reset();
			}else{
				// 创建弹框
				this.createAsync('popwin', 'sweety/popwin/verity.verityWin', {

				}, function(mod){
					mod.show().setParam(id, title, QualificationId).reset();
				});
			}
			return this;
		},
		onSearch: function(ev){
			this.$.grid.setParam({'word': ev.param}).reload();
			return false;
		}
	});
	exports.sweety = SweetyVerify;

	// 创意审核
	var CreativeVerify = Base.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-verifyCreative'
			});

			this.Super('init', arguments);
		},
		reload: function(){
			this.$.grid.reload();
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			el.append($([
				// '<div class="M-formHead">',
				// 	'<div class="M-formHeadTitle">'+LANG('创意审核')+'</div>',
				// '</div>',
				'<div>',
					'<div class="P-verifyGridHead">',
						'<span class="" pub-mod="@base/common/drop.drop" pub-name="aptitude"></span>',
						'<span class="ml15">',
							'<label>' + '<input type="checkbox"/>' + LANG('只显示审核未通过的创意') +'</label>',
						'</span>',
					'</div>',
					'<div pub-name="grid" pub-mod="sweety/grid/verify.creative"/>',
				'</div>'
			].join('')));


			self.buildFromTemplate({
				'aptitude': {
					'options': this.$AptitudeItems,
					'value': this.$def_aptitudeId
				},
				'grid': {
					'metrics': this.$metrics,
					'hasSearch': true,
					'hasFilter': false,
					'eventDataLoad': true,
					'param': {
						'QualificationId': this.$def_aptitudeId
					},
					'auto_load': !!this.$def_aptitudeId
				}
			});

			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'attr': {'data-tab': this._.name},
				'items': [

				]
			});
		},
		onSearch: function(ev)
		{
			this.$.grid.setParam({'word': ev.param}).reload();
			return false;
		}
	});
	exports.creative = CreativeVerify;
});